package com.jjwxc.jwjskandriod.framework.imageloader;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.signature.ObjectKey;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.framework.utils.FFLogUtil;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;

import java.io.File;

public class FFImageLoader {
    /**
     * 加载网络图片带参数（加载中，加载失败，的默认图标）
     */
    public static void loadImage(Context context, ImageView imageView, String url, int loading, int error) {
        RequestOptions options = new RequestOptions().skipMemoryCache(false).placeholder(loading).error(error);
        Glide.with(context).asDrawable().apply(options).load(url).into(imageView);
    }

    /**
     * 加载网络图片
     */
    public static void loadImage(Context context, ImageView imageView, String url, String tag) {
        FFLogUtil.d("image", url + tag);
        RequestOptions options = new RequestOptions().skipMemoryCache(false).error(R.mipmap.imgmoren_icon)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).signature(new ObjectKey(tag));
        Glide.with(context).asDrawable().transition(DrawableTransitionOptions.withCrossFade())
                .apply(options).load(url).into(imageView);
    }

    /**
     * 加载网络图片是否是半圆角和圆角
     */
    public static void loadImage(Context context, ImageView imageView, String url, String tag, boolean Round) {
        FFLogUtil.d("image", url + tag);
        RequestOptions options = new RequestOptions().skipMemoryCache(false).error(R.mipmap.imgmoren_icon)
                .transform(new GlideRoundTransform(context.getResources().getDimension(R.dimen.size_dp_5), Round))
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).signature(new ObjectKey(tag));
        Glide.with(context).asDrawable()
                .apply(options).load(url).into(imageView);
    }


    /**
     * 加载本地图片是否是半圆角和圆角
     */
    public static void loadImage(Context context, ImageView imageView, int url, boolean Round) {
        FFLogUtil.d("image", url + "");
        RequestOptions options = new RequestOptions()
                .transform(new GlideRoundTransform(context.getResources().getDimension(R.dimen.size_dp_15), Round))
                .skipMemoryCache(false);
        Glide.with(context).asDrawable()
                .apply(options).load(url).into(imageView);
    }


    /**
     * 加载网络不需要缓存图片
     */
    public static void loadImage(Context context, ImageView imageView, String url) {
        if (StUtils.isEmpty(url) || imageView == null) {
            return;
        }
        RequestOptions options = new RequestOptions().skipMemoryCache(false).error(R.mipmap.imgmoren_icon);
        Glide.with(context).asDrawable().apply(options).load(url).into(imageView);
    }

    /**
     * 加载网络不需要缓存图片
     */
    public static void loadCircleImage(Context context, ImageView imageView, String url) {
        if (StUtils.isEmpty(url) || imageView == null) {
            return;
        }
        Glide.with(context).load(url).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(imageView);
    }

    /**
     * 加载网络图片是否是半圆角和圆角
     */
    public static void loadImageNoCache(Context context, ImageView imageView, String url, boolean Round) {
        FFLogUtil.d("image", url);
        RequestOptions options = new RequestOptions().skipMemoryCache(false)
                .transform(new GlideRoundTransform(context.getResources().getDimension(R.dimen.size_dp_5), Round));
        Glide.with(context).asDrawable()
                .apply(options).load(url).into(imageView);
    }

    /**
     * 加载本地图片
     */
    public static void loadImage(Context context, ImageView imageView, String filePath, boolean sd) {
        File file = new File(Environment.getExternalStorageDirectory(), filePath);
        if (file != null && file.exists()) {
            Glide.with(context).load(file).into(imageView);
        }

    }

    /**
     * 加载应用资源
     */
    public static void loadImage(Context context, ImageView imageView, int resource) {
        Glide.with(context).load(resource).into(imageView);


    }

    /**
     * 加载Uri对象
     */
    public static void loadImage(Context context, ImageView imageView, Uri imageUri) {
        Glide.with(context).load(imageUri).into(imageView);
    }

    @SuppressLint("CheckResult")
    public static int getImageWidth(Context context, String url) {
        final int[] width = new int[1];
        RequestOptions options = new RequestOptions().skipMemoryCache(false);
        Glide.with(context).asBitmap().load(url).apply(options)
                .listener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap bitmap, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        width[0] = bitmap.getWidth();
                        return false;
                    }
                });
        return width[0];
    }


}
